﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Design;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.ComboBox;

namespace WenSkin.Controls
{
    public class WenLableComboBox : WenControl
    {
        public WenLableComboBox()
        {
            wenComboBox = new WenComboBox();
            wenComboBox.SelectedIndexChanged += (s, e) =>
            {
                base.Text = wenComboBox.Text;
            };
            wenComboBox.TextChanged += (s, e) =>
            {
                base.Text = wenComboBox.Text;
            };
            wenComboBox.SizeChanged += (s, e) =>
            {
                this.Height = wenComboBox.Height;
            };

            this.Controls.Add(wenComboBox);
        }

        #region 私有属性

        private WenComboBox wenComboBox;

        private string textLable;
        #endregion

        #region 公有属性



        public override string Text { get => base.Text; set { base.Text = value; wenComboBox.Text = value; } }

        [Category("Wen"), Description("下拉选项组合功能")]
        [DefaultValue(typeof(ComboBoxStyle), "DropDown")]
        public ComboBoxStyle DropDownStyle { get => wenComboBox.DropDownStyle; set => wenComboBox.DropDownStyle = value; }

        [Category("Wen"), Description("标签显示文本内容")]
        public string TextLable
        {
            get => textLable;
            set
            {
                textLable = value;
                this.Invalidate();
                SetLocationSize();
            }
        }

        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        [Editor("System.Windows.Forms.Design.ListControlStringCollectionEditor", typeof(UITypeEditor))]
        [Localizable(true)]
        [MergableProperty(false)]
        public ObjectCollection Items => wenComboBox.Items;
        #endregion

        private void SetLocationSize()
        {
            int w = TextRenderer.MeasureText(TextLable, this.Font).Width + 3;
            int h = (this.Height - wenComboBox.Height) / 2;
            wenComboBox.Location = new Point(w, h < 0 ? 0 : h);
            wenComboBox.Size = new Size(this.Width - w, (this.Height - wenComboBox.Height) / 2);
        }

        #region 重写事件

        //保持控件宽度
        protected override void SetBoundsCore(int x, int y, int width, int height, BoundsSpecified specified)
        {
            base.SetBoundsCore(x, y, width, wenComboBox.Height, specified);
            SetLocationSize();
        }

        #endregion

        #region 重绘

        protected override void WenOnPaint(Graphics g, Rectangle rec, PaintEventArgs e)
        {
            base.WenOnPaint(g, rec, e);
            this.DrawString(this.TextLable, g, rec);
        }

        #endregion

        public new void Focus()
        {
            wenComboBox.Focus();
        }
    }
}
